package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.OptionUserFKAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.HomeIndulgenceBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.ItemDecoration;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 用户反馈
 * */
public class OptionUserFKActivity extends BaseActivity {

    @BindView(R.id.activity_userfk_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.activity_userfk_editText)
    EditText editText;

    private OptionUserFKAdapter adapter;
    private String[] strings = {"租车问题", "押金问题", "买车问题", "换电问题", "电池问题", "APP软件问题", "售后问题", "电柜问题", "其他"};
    private boolean[] booleans = {false, false, false, false, false, false, false, false, false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userfk);
        ButterKnife.bind(this);

        initView();
        initListener();

    }

    private void initListener() {
        adapter.setOnItemClickListener(new OptionUserFKAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (booleans[position]) {
                    booleans[position] = !booleans[position];
                    adapter.notifyDataSetChanged();
                    return;
                }
                int num = 0;
                for (int i = 0; i < booleans.length; i++) {
                    if (booleans[i]) {
                        num++;
                    }
                }
                if (num < 3) {
                    booleans[position] = !booleans[position];
                    adapter.notifyDataSetChanged();
                } else {
                    toast("最多只能选择3个问题！");
                }
            }
        });
    }

    private void initView() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.addItemDecoration(new ItemDecoration(15, 15, 15, 15));
        adapter = new OptionUserFKAdapter(this, strings, booleans);
        recyclerView.setAdapter(adapter);
    }

    @OnClick({R.id.activity_userfk_back, R.id.activity_userfk_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_userfk_back:
                finish();
                break;
            case R.id.activity_userfk_submit:
                String s = "";
                for (int i = 0; i < booleans.length; i++) {
                    if (booleans[i]) {
                        if (s.equals("")) {
                            s += strings[i];
                        } else {
                            s += "," + strings[i];
                        }
                    }
                }

                if (s.equals("")) {
                    toast("请选择问题类型！");
                    return;
                }
                if (editText.getText().toString().trim().equals("")) {
                    toast("请填写问题描述！");
                    return;
                }
                initSubmit(s, editText.getText().toString().trim());
                break;
        }
    }

    private void initSubmit(String s, String content) {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.feedback, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("用户反馈", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        finish();
                        toast(msg);
                    } else
                        toast(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("cityId", Constants.CITYID);
                    json_url.put("title", s);
                    json_url.put("content", content);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }
}
